#!/bin/bash -x

# In this case, the upstream project seems to already
# provide binaries that are supposed to run everywhere,
# so we merely package these as an AppImage

set +e

URL=$(wget -q "https://www.blender.org/download/" -O - | grep -Poe "http.*?-x86_64.tar.bz2" | tail -n 1)
echo $URL

APP=Blender

mkdir -p ./$APP/$APP.AppDir/usr/bin
cd ./$APP

wget -q https://github.com/AppImage/AppImages/raw/master/functions.sh -O ./functions.sh
. ./functions.sh

wget -c "$URL"

tar xf *.tar.bz2
mv blender-*/* $APP.AppDir/usr/bin/

cd $APP.AppDir/

get_apprun

# Move icons into place so that AppImageAssistant finds them
cp ./usr/bin/icons/scalable/apps/blender.svg .

# Get appdata
mkdir -p ./usr/share/metainfo/
wget -q "https://raw.githubusercontent.com/hughsie/fedora-appstream/master/appdata-extra/desktop/blender.appdata.xml" -O ./usr/share/metainfo/blender.appdata.xml

# Get desktop file
# wget -q "https://git.blender.org/gitweb/gitweb.cgi/blender.git/blob_plain/HEAD:/release/freedesktop/blender.desktop"
# Workaround for:
# Segmentation fault when running AppRun
cat > blender.desktop <<\EOF
[Desktop Entry]
Name=Blender
GenericName=3D modeler
Comment=3D modeling, animation, rendering and post-production
Keywords=3d;cg;modeling;animation;painting;sculpting;texturing;video editing;video tracking;rendering;render engine;cycles;game engine;python;
Exec=blender %f
Icon=blender
Terminal=false
Type=Application
Categories=Graphics;3DGraphics;
MimeType=application/x-blender;
EOF

# Add desktop integration
XAPP=blender
wget -O ./usr/bin/$XAPP.wrapper https://raw.githubusercontent.com/probonopd/AppImageKit/master/desktopintegration
chmod a+x ./usr/bin/$XAPP.wrapper
sed -i -e "s|Exec=$XAPP|Exec=$XAPP.wrapper|g" $XAPP.desktop

# Go out of AppImage
cd ..

# Figure out $VERSION
VERSION=$(ls blender-*.bz2 | cut -d "-" -f 2)
echo $VERSION

generate_appimage
